package com.huike.web.controller.report;


import com.huike.common.utils.SecurityUtils;
import org.apache.commons.lang3.SerializationUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.huike.common.core.domain.AjaxResult;
import com.huike.report.service.IReportService;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private IReportService reportService;


    /**
     * 首页--基础数据统计
     * @param beginCreateTime
     * @param endCreateTime
     * @return
     */
    @GetMapping("/getBaseInfo")
    public AjaxResult getBaseInfo(@RequestParam("beginCreateTime") String beginCreateTime,
                                  @RequestParam("endCreateTime") String endCreateTime){
        return AjaxResult.success(reportService.getBaseInfo(beginCreateTime,endCreateTime));
    }


    @GetMapping()
    public AjaxResult getResult(@RequestParam("beginCreateTime") String beginCreateTime,
                                @RequestParam("endCreateTime") String endCreateTime
                               ){

        // HashMap<String,Integer> allMap=reportService.selectAllNum(beginCreateTime,endCreateTime);
        // HashMap<String,Integer> todayMap=reportService.selectTodayNum(beginCreateTime,endCreateTime);
        // HashMap<String,Integer> waitMap=reportService.selectWaitNum();

        AjaxResult ajaxResult= null;
        try {
            ajaxResult = new AjaxResult();

            String username = SecurityUtils.getUsername();
            CompletableFuture<HashMap<String,Integer>> allMap = CompletableFuture.supplyAsync(() -> {
                //线索数量
                return reportService.selectAllNum(beginCreateTime,endCreateTime);

            });
            CompletableFuture<HashMap<String,Integer>> todayMap = CompletableFuture.supplyAsync(() -> {
                //线索数量
                return reportService.selectTodayNum(beginCreateTime,endCreateTime);

            });
            CompletableFuture<HashMap<String,Integer>> waitMap = CompletableFuture.supplyAsync(() -> {
                //线索数量
                return reportService.selectWaitNum(username);

            });


            CompletableFuture.
                    allOf(
                            allMap,
                            todayMap,
                            waitMap)
                    .join();

            ajaxResult.putAll( allMap.get());
            ajaxResult.putAll(todayMap.get());
            ajaxResult.putAll(waitMap.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return AjaxResult.success(ajaxResult);
    }

}